<template>
  <a-modal
    :title="modalTitle"
    style="top: 20px"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display: none">
        <a-input v-decorator="['id']" />
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="上级部门">
        <a-tree-select
          v-decorator="['parentId', { initialValue: 0, rules: [{ required: true, message: '请选择上级部门' }] }]"
          :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
          :treeData="depts"
          placeholder="上级部门"
          treeDefaultExpandAll
        >
        </a-tree-select>
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="部门名称">
        <a-input
          v-decorator="['deptName', { rules: [{ required: true, message: '请输入部门名称' }] }]"
          placeholder="请输入部门名称"
        />
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="显示顺序">
        <a-input
          v-decorator="['sort', { rules: [{ required: true, message: '请输入显示顺序' }] }]"
          placeholder="显示顺序"
        />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="负责人">
        <a-input
          v-decorator="['leader', { rules: [{ required: true, message: '请输入负责人' }] }]"
          placeholder="负责人"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { getDeptList, saveDept } from '@/api/system/sysDept'
import pick from 'lodash.pick'
export default {
  name: 'DeptModal',
  components: {},
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      depts: [{ key: '0', value: '0', title: '无' }],
      mdl: {},
      confirmLoading: false,
      form: this.$form.createForm(this),
      modalTitle: undefined,
    }
  },
  beforeCreate() {},
  created() {
    getDeptList().then((res) => {
      this.buildtree(res.result, this.depts, 0)
    })
  },
  methods: {
    add(parentId) {
      this.modalTitle = '新增部门'
      this.form.resetFields()
      this.edit({ parentId: parentId || 0 })
    },
    edit(record) {
      if (record.id > 0) {
        this.modalTitle = '编辑部门'
      }
      this.mdl = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.mdl.parentId += ''
        this.form.setFieldsValue(pick(this.mdl, 'id', 'parentId', 'leader', 'sort', 'deptName'))
        // this.form.setFieldsValue({ ...record })
      })
    },
    buildtree(list, arr, parentId) {
      list.forEach((item) => {
        if (item.parentId === parentId) {
          var child = {
            key: item.id,
            value: item.id + '',
            title: item.deptName,
            children: [],
          }
          this.buildtree(list, child.children, item.id)
          arr.push(child)
        }
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.$emit('ok')
          this.visible = false
          this.confirmLoading = true
          saveDept(values)
            .then((res) => {
              if (res.code === 200) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch(() => {
              this.$message.error('系统错误，请稍后再试')
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  },
}
</script>
